<template>
  <el-dialog :visible.sync="isShow" :title="title" @close="btns">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model.trim="form.name" type="text" />
      </el-form-item>
      <el-form-item prop="code" label="部门编码">
        <el-input v-model="form.code" type="text" />
      </el-form-item>
      <el-form-item prop="manager" label="部门负责人">
        <!-- <el-input type="text" /> -->
        <el-select v-model="form.manager" @focus="focusevent">
          <el-option v-for="item in userLift" :key="item.id" :value="item.username">{{ item.username }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="introduce" label="部门介绍">
        <el-input v-model="form.introduce" type="text" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div>
        <el-button @click="btns">取消</el-button>
        <el-button @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
// 导入员工基本信息
import { sysUsersimple, companyDepartmentPost, companyDepartmentPut } from '@/api/departments.js'
export default {
  props: {
    inittreeData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      mode: 'add',
      isShow: false,
      itemData: [],
      userLift: [],
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'blur' },
          // { validator: (rule, value, callback) => {
          //   const tempArr = this.inittreeData.filter((item) => {
          //     return item.pid === this.itemData.id
          //   })
          //   const bol = tempArr.some((item) => {
          //     return item.name === value
          //   })
          //   bol ? callback(new Error('部门重名:' + value)) : callback()
          // } }
          { validator: (rule, value, callback) => {
            const tempArr = this.inittreeData.filter((item) => {
              if (this.mode === 'add') {
                return item.pid === this.itemData.id
              } else if (this.mode === 'edit') {
                return (
                  item.pid === this.itemData.pid &&
                  item.id !== this.itemData.id
                )
              }
            })
            const bol = tempArr.some((item) => {
              return item.name === value
            })
            bol ? callback(new Error('部门名称重名:' + value)) : callback()
          }
          }
        ],
        code: [{ required: true, message: '必填', trigger: 'blur' },
        //  { validator: (rule, value, callback) => {
        //   const bol = this.inittreeData.some((item) => {
        //     return item.code === value
        //   })
        //   bol ? callback(new Error()) : callback()
        // } }
          { validator: (rule, value, callback) => {
            const bol = this.inittreeData.some((item) => {
              if (this.mode === 'add') {
                return item.code === value
              } else if (this.mode === 'edit') {
                return item.code === value && item.id !== this.itemData.id
              }
            })
            bol ? callback(new Error('部门编码重复')) : callback()
          }
          }
        ],
        manager: [{ required: true, message: '必填', trigger: 'blur' }],
        introduce: [{ required: true, message: '必填', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return {
        add: '新增部门',
        edit: '编辑部门'
      }[this.mode]
    }
  },
  // watch: {
  //   isShow(newVal) {
  //     if (!newVal) {
  //       this.isShow = false
  //       this.$refs.form.resetFields()
  //       this.form = {
  //         name: '',
  //         code: '',
  //         manager: '',
  //         introduce: '',
  //         pid: ''
  //       }
  //     }
  //   }
  // },
  mounted() {
    this.$but.$on('changeShow', (bol, itemData, mode) => {
      // console.log(11)
      this.itemData = itemData
      // 把父元素的id给子元素
      this.form.pid = itemData.id
      // 接收场景数据
      this.mode = mode
      // 如果是编辑,数据要回显,深拷贝当时点击项数据
      if (mode === 'edit') {
        this.form = JSON.parse(JSON.stringify(itemData))
      }
      this.isShow = bol
    })
  },
  beforeDestroy() {
    this.$but.$off('changeShow')
  },
  methods: {
    btns() {
      this.isShow = false
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
    },
    async focusevent() {
      if (!this.userLift.length) {
        const res = await sysUsersimple()
        this.userLift = res.data
        console.log(res, '员工信息')
      }
    },
    submit() {
      this.$refs.form.validate(async(val) => {
        if (val) {
          if (this.mode === 'add') {
            await companyDepartmentPost(this.form)
            this.$message.success('添加成功')
          } else if (this.mode === 'edit') {
            await companyDepartmentPut(this.form)
            this.$message.success('添加成功')
          }
          this.isShow = false
          // 刷新页面,通过固定写法,
          this.$emit('getDate')
        }
      })
    }
  }
}
</script>

<style>

</style>
